/**
  ******************************************************************************
  * @file    bmi088.h
  * @brief   This file contains all the function prototypes for
  *          the bmi088.c file
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __BMI088_H__
#define __BMI088_H__

#include "main.h"

#include "math.h"

#ifdef USE_FREERTOS
#include "FreeRTOS.h"
#include "task.h"
#endif

#define BMI088_ACC_SOFTRESET					0x7E
#define BMI088_ACC_PWR_CTRL						0x7D
#define BMI088_ACC_PWR_CONF						0x7C
#define BMI088_ACC_SELF_TEST					0x6D
#define BMI088_ACC_INT_MAP_DATA				0x58
#define BMI088_ACC_INT2_IO_CTRL				0x54
#define BMI088_ACC_INT1_IO_CTRL				0x53
#define BMI088_ACC_FIFO_CONFIG_1			0x49
#define BMI088_ACC_FIFO_CONFIG_2			0x48
#define BMI088_ACC_FIFO_WTM_1					0x47
#define BMI088_ACC_FIFO_WTM_0					0x46
#define BMI088_ACC_FIFO_DOWNS					0x45
#define BMI088_ACC_RANGE							0x41
#define BMI088_ACC_CONF								0x40
#define BMI088_ACC_FIFO_DATA					0x26
#define BMI088_ACC_FIFO_LENGTH_1			0x25
#define BMI088_ACC_FIFO_LENGTH_0			0x24
#define BMI088_ACC_TEMP_LSB						0x23
#define BMI088_ACC_TEMP_MSB						0x22
#define BMI088_ACC_INT_STAT_1					0x1D
#define BMI088_ACC_SENSORTIME_2				0x1A
#define BMI088_ACC_SENSORTIME_1				0x19
#define BMI088_ACC_AENSORTIME_0				0x18
#define BMI088_ACC_Z_MSB							0x17
#define BMI088_ACC_Z_LSB							0x16
#define BMI088_ACC_Y_MSB							0x15
#define BMI088_ACC_Y_LSB							0x14
#define BMI088_ACC_X_MSB							0x13
#define BMI088_ACC_X_LSB							0x12
#define BMI088_ACC_STATUCS						0x03
#define BMI088_ACC_ERR_REG						0x02
#define BMI088_ACC_CHIP_ID						0x00


#define BMI088_GYRO_FIFO_DATA					0x3F
#define BMI088_GYRO_FIFO_CONFIG_1			0x3E
#define BMI088_GYRO_FIFO_CONFIG_0			0x3D
#define BMI088_GYRO_SELF_TEST					0x3C
#define BMI088_GYRO_FIFO_EXT_INT_S		0x34
#define BMI088_GYRO_FIFO_WM_EN				0x1E
#define BMI088_GYRO_INT3_INT4_IO_MAP	0x18
#define BMI088_GYRO_INT3_INT4_IO_CONF	0x16
#define BMI088_GYRO_INT_CTRL					0x15
#define BMI088_GYRO_SOFTRESET					0x14
#define BMI088_GYRO_LPM1							0x11
#define BMI088_GYRO_BANDWIDTH					0x10
#define BMI088_GYRO_RANGE							0x0F
#define BMI088_GYRO_FIFO_STATUS				0x0E
#define BMI088_GYRO_INT_STAT_1				0x0A
#define BMI088_GYRO_RATE_Z_MSB				0x07
#define BMI088_GYRO_RATE_Z_LSB				0x06
#define BMI088_GYRO_RATE_Y_MSB				0x05
#define BMI088_GYRO_RATE_Y_LSB				0x04
#define BMI088_GYRO_RATE_X_MSB				0x03
#define BMI088_GYRO_RATE_X_LSB				0x02
#define BMI088_GYRO_CHIP_ID						0x00

#define BMI088_ACC_RANGE_3 						0x00
#define BMI088_ACC_RANGE_6 						0x01
#define BMI088_ACC_RANGE_12 					0x02
#define BMI088_ACC_RANGE_24 					0x03

#define BMI088_GYRO_RANGE_2000				0x00
#define BMI088_GYRO_RANGE_1000				0x01
#define BMI088_GYRO_RANGE_500					0x02
#define BMI088_GYRO_RANGE_250					0x03
#define BMI088_GYRO_RANGE_125					0x04

#define BMI088_PI 3.14159265
#define BMI088_G 9.80665

typedef struct
{
	struct
	{
		int16_t x;
		int16_t y;
		int16_t z;
	}acc_data;
	struct
	{
		int16_t x;
		int16_t y;
		int16_t z;
	}gyro_rate_data;
	struct
	{
		float x;
		float y;
		float z;
	}acc;
	struct
	{
		float x;
		float y;
		float z;
	}gyro_rate;
	
	float temp;
	
	struct
	{
		float x;
		float y;
		float z;
	}acc_offset;
	
	struct
	{
		float x;
		float y;
		float z;
	}gyro_offset;
	
	SPI_HandleTypeDef* hspi_ptr;
	GPIO_TypeDef* GPIO_acc_cs;
	uint16_t GPIO_PIN_acc_cs;
	GPIO_TypeDef* GPIO_gyro_cs;
	uint16_t GPIO_PIN_gyro_cs;
	
	uint8_t acc_range;
	uint8_t gyro_range;
	float temp_target;
	
}bmi088_struct;

extern bmi088_struct bmi088_data;

void bmi088_init(bmi088_struct* bmi088, SPI_HandleTypeDef* hspi_ptr, GPIO_TypeDef* GPIO_acc_cs, uint16_t GPIO_PIN_acc_cs, GPIO_TypeDef* GPIO_gyro_cs, uint16_t GPIO_PIN_gyro_cs);
void bmi088_range_set(bmi088_struct* bmi088, uint8_t acc_range, uint8_t gyro_range);
void bmi088_temp_set(bmi088_struct* bmi088, float temp);
void bmi088_gyro_offset_set(bmi088_struct* bmi088, float x, float y, float z);
void bmi088_acc_read(bmi088_struct* bmi088);
void bmi088_gyro_read(bmi088_struct* bmi088);
void bmi088_temp_ctrl(bmi088_struct* bmi088);
void bmi088_gyro_calibration(bmi088_struct* bmi088);
void bmi088_acc_ready_callback(bmi088_struct* bmi088);
void bmi088_gyro_ready_callback(bmi088_struct* bmi088);

#endif